/*
 * Copyright (C) 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ch_linghu.fanfoudroid.data;

import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.ch_linghu.fanfoudroid.R;
import com.ch_linghu.fanfoudroid.TwitterApplication;
import com.ch_linghu.fanfoudroid.fanfou.Status;
import com.ch_linghu.fanfoudroid.util.DateTimeHelper;
import com.ch_linghu.fanfoudroid.util.TextHelper;

public class Tweet extends Message implements Parcelable {
	private static final String TAG = "Tweet";

	public com.ch_linghu.fanfoudroid.fanfou.User user;
	public String source;
	public String prevId;
	private int statusType = -1; // @see StatusTable#TYPE_*

	public void setStatusType(int type) {
		statusType = type;
	}

	public int getStatusType() {
		return statusType;
	}

	public Tweet() {
	}

	public static Tweet create(Status status) {
		Tweet tweet = new Tweet();

		tweet.id = status.getId();
		// 转义符放到getSimpleTweetText里去处理，这里不做处理
		tweet.content = TextHelper.getSimpleTweetText(status.getContent());
		tweet.ctime = status.getCtime();
//		tweet.favorited = status.isFavorited() ? "true" : "false";
//		tweet.truncated = status.isTruncated() ? "true" : "false";
		tweet.comment = status.getComment();
		tweet.favorited = status.getFavorited();
		 
		tweet.from = status.getFrom();

		tweet.uname = TextHelper.getSimpleTweetText(status.getUser()
				.getUname());
		tweet.face = status.getUser().getFace()
				.toString();
		tweet.userId = status.getUser().getId();
		tweet.user = status.getUser();
        tweet.type  =status.getType();
        tweet.city =status.getCity();
        tweet.bid  = status.getBid();
        tweet.bname = status.getBname();
     
		tweet.thumbnail_pic = status.getThumbnail_pic();
		tweet.bmiddle_pic = status.getBmiddle_pic();
		tweet.original_pic = status.getOriginal_pic();

	//	tweet.source = TextHelper.getSimpleTweetText(status.getSource());

		return tweet;
	}

	public static Tweet createFromSearchApi(JSONObject jsonObject)
			throws JSONException {
		Tweet tweet = new Tweet();

		tweet.id = jsonObject.getString("weibo_id") + "";
		// 转义符放到getSimpleTweetText里去处理，这里不做处理
		tweet.content = jsonObject.getString("content");
		tweet.ctime = DateTimeHelper.parseSearchApiDateTime(jsonObject
				.getString("ctime"));
		//tweet.favorited = jsonObject.getInt("favorited");
		tweet.from = jsonObject.getInt("from");
		tweet.comment = jsonObject.getInt("comment");
		tweet.transpondId = jsonObject.getInt("transpond_id");
		tweet.transpond = jsonObject.getInt("transpond");
		tweet.type = jsonObject.getInt("type");
		tweet.city = jsonObject.getInt("city");
		tweet.bid = jsonObject.getInt("bid");
		tweet.bname = jsonObject.getString("bname");

//		tweet.screenName = TextHelper.getSimpleTweetText(jsonObject
//				.getString("from_user"));
//		tweet.profileImageUrl = jsonObject.getString("profile_image_url");
		tweet.userId = jsonObject.getString("from_user_id");
//		tweet.source = TextHelper.getSimpleTweetText(jsonObject
//				.getString("source"));

		return tweet;
	}

	public static String buildMetaText(StringBuilder builder, Date createdAt,
			String source, String replyTo) {
		builder.setLength(0);

		builder.append(DateTimeHelper.getRelativeDate(createdAt));
		builder.append(" ");
		
		if (!TextUtils.isEmpty(source)) {
			builder.append(TwitterApplication.mContext.getString(R.string.tweet_source_prefix));
		
			builder.append(source);
		}

//		if (!TextUtils.isEmpty(replyTo)) {
//			builder.append(" "
//					+ TwitterApplication.mContext
//							.getString(R.string.tweet_reply_to_prefix));
//			builder.append(replyTo);
//			builder.append(TwitterApplication.mContext
//					.getString(R.string.tweet_reply_to_suffix));
//		}

		return builder.toString();
	}

	// For interface Parcelable

	public int describeContents() {
		return 0;
	}

	public void writeToParcel(Parcel out, int flags) {
		out.writeString(id);
		out.writeString(content);
		out.writeValue(ctime); // Date
		out.writeString(uname);
		out.writeString(face);
		out.writeString(userId);

		out.writeInt(from);
		out.writeInt(comment);
		out.writeInt(transpondId);
		out.writeInt(transpond);
		out.writeInt(type);
		out.writeInt(city);
		out.writeInt(bid);
		out.writeString(bname);
		out.writeString(thumbnail_pic);
		out.writeString(bmiddle_pic);
		out.writeString(original_pic);

//		out.writeString(source);
	}

	public static final Parcelable.Creator<Tweet> CREATOR = new Parcelable.Creator<Tweet>() {
		public Tweet createFromParcel(Parcel in) {
			return new Tweet(in);
		}

		public Tweet[] newArray(int size) {
			// return new Tweet[size];
			throw new UnsupportedOperationException();
		}
	};

	public Tweet(Parcel in) {
		id = in.readString();
		content = in.readString();
		ctime = (Date) in.readValue(Date.class.getClassLoader());
		uname = in.readString();
		face = in.readString();
		userId = in.readString();
//		favorited = in.readString();
		from = in.readInt();
		comment = in.readInt();
		transpondId = in.readInt();
		transpond = in.readInt();
		type = in.readInt();
		city =in.readInt();
		bid =in.readInt();
		bname =in.readString();
		thumbnail_pic = in.readString();
		bmiddle_pic = in.readString();
		original_pic = in.readString();
		
//		source = in.readString();
	}

	@Override
	public String toString() {
		return "Tweet [content=" + content + ", id=" + id + ", uname="
				+ uname + ", face=" + face + ", ctime="
				+ ctime + ", userId=" + userId + ", from="
				+ from + ", comment=" + comment + ", transpondId="
				+ transpondId + ", transpond=" + transpond
				+ ", type=" + type + ", city=" + city + ", bid=" + bid+ ", bname=" + bname  
				+ ", thumbnail_pic=" + thumbnail_pic 
				+ ", bmiddle_pic=" + bmiddle_pic 
				+ ", original_pic=" + original_pic 
				+ "]";
	}

}
